package net.scribblemedia.sqldsk.service;

import java.util.Date;

import net.scribblemedia.sqldsk.domain.Comment;
import net.scribblemedia.sqldsk.domain.ReportQuery;
import net.scribblemedia.sqldsk.domain.ReportRow;
import net.scribblemedia.sqldsk.domain.ReportRowState;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ReportRowService {

	@Transactional
	public void addComment(ReportRow reportRow, String commentText) {
		Comment comment = new Comment();
		comment.setText(commentText);
		comment.setCommentDate(new Date());
		comment.persist();
		reportRow.addComment(comment);
	}
	
	@Transactional
	public void deleteComment(Comment comment) {
		comment.getReportRow().removeComment(comment);
		comment.remove();
	}

	@Transactional
	public void updateReportRowState(ReportRow reportRow, ReportRowState reportRowState) {
		reportRow.setReportRowState(reportRowState);
		addComment(reportRow, "Transitioned to state: " + reportRowState);
	}

	@Transactional
	public void toggleShowHideArchivedRows(ReportQuery reportQuery) {
		reportQuery.setShowArchivedRows(!reportQuery.isShowArchivedRows());
	}

	@Transactional
	public void toggleArchivedState(ReportRow reportRow) {
		reportRow.setArchived(!reportRow.isArchived());
		addComment(reportRow, "Transitioned to state: " + (reportRow.isArchived() ? "ARCHIVED" : "UN-ARCHIVED"));
	}
}
